require.config({
    baseUrl: '/static',
    paths: {
        jquery: './lib/jquery',
        bootstrap: './lib/bootstrap.min',
        jqueryDataTables: './lib/jqueryDataTables',
        dataTablesBootstrap: './lib/dataTablesBootstrap',
        newIframe: './lib/common/newIframe', // 生成标签页
        tableOption: './lib/common/tableOption', // 列表内容选择 ， 删除等操作
        preventBack: './lib/common/preventBack', //阻止浏览器后退
        validate: './lib/jquery.validate.min',
        messagesZh: './lib/messagesZh',
        validateExtend: './lib/common/validateExtend',
        checkSelect: './lib/common/checkSelect',
        checkOne: './lib/common/checkSingleSelect',
        jqueryUI: './js/jquery-ui',
        ajaxTable: './module/user/userTable',
        tableSearch: './lib/common/tableSearch'

    },

    shim: {
        bootstrap: {
            deps: ['jquery'],
            exports: 'bootstrap'
        },
        validate: {
            deps: ['jquery'],
            exports: 'validate',
        },
        validateExtend: {
            deps: ['jquery', 'validate'],
            exports: 'validateExtend'
        },
        messagesZh: {
            deps: ['jquery', 'validate'],
            exports: 'messagesZh'
        },
        jqueryDataTables: {
            deps: ['jquery'],
            exports: 'jqueryDataTables',
        },

        dataTablesBootstrap: {
            deps: ['bootstrap', 'jqueryDataTables'],
            exports: 'dataTablesBootstrap'
        },
        uploadImg: {
            deps: ['jquery'],
            exports: 'uploadImg'
        },
        checkSelect: {
            deps: ['jquery'],
            exports: 'checkSelect'
        },
        checkOne: {
            deps: ['jquery'],
            exports: 'checkOne'
        },
        jqueryUI: {
            deps: ['jquery'],
            exports: 'jqueryUI'
        },
        ajaxTable: {
            deps: ['jquery', 'jqueryDataTables', 'dataTablesBootstrap'],
            exports: 'ajaxTable'
        },
        tableSearch: {
            deps: ['jquery', 'jqueryDataTables'],
            exports: 'tableSearch'
        }
    }
});

define(['jquery', 'checkSelect', 'checkOne', 'tableSearch', 'bootstrap', 'jqueryDataTables', 'dataTablesBootstrap', 'ajaxTable', 'newIframe', 'tableOption', 'preventBack', 'validate',  'messagesZh', 'validateExtend','jqueryUI'], function($, checkSelect, checkOne, tableSearch) {
    //表单验证
    $('form').validate({
        submitHandler: function (form) {
            if ($("#default_img").find(".img_item").length == 0) {
                return alert("封面必须上传")
            } else {
                form.submit();
            }
        }
    });

    // 预览图片
    $("body").on("click", ".previewImg", function() {
        var imgs = $(this).data("src").split(",");

        console.log('click', imgs)
        $("img#contract-img").attr("src", $(this).data("src"))
        $("#img_modal").modal('show')
    })

    //图片上传
    $(function() {
        // 如果图片已经到达了上传的最大张数，添加图片按钮隐藏
        $(".img-box").each(function(index, val) {
            console.log($(val).data("num"), $(val).find(".img_item").length)
            if (parseInt($(val).data("num")) == $(val).find(".img_item").length) {
                $(val).find(".add_img").hide()
            }
        })
        var maxSum = 1; //最多上传的图片张数
        var goods_img_num = $(".img_item").length;

        // 上传图片
        $('body').on('click', '.add_img', function() {
            name = $(this).attr('name');
            maxSum = $(this).parents(".img-box").data("num");
            console.log('maxSum', maxSum)
            $(this).siblings('input[type="file"]').click();
        });
        $('body').on('change', 'input[type="file"]', function(e) {
            var _this = $(this);
            $('.mud').find('div').text("正在上传...");
            $(".mud").show();
            var add_img = $(this).siblings('.add_img');
            var file = e.target.files;
            if (file) {
                var reader = new FileReader();
                reader.onload = function(evt) {
                    $.post(uploadUrl, {
                        'imageData': evt.target.result
                    }, function(json) {
                        console.log(json);
                        json = $.parseJSON(json);
                        var tem = '<div class="img_item">' +
                            '<img src="' + json.url + '">' +
                            '<input type="hidden" name="' + name + '" value="' + json.url + '" >' +
                            '<a class="del_img del">×</a>' +
                            '</div>';
                        add_img.before(tem);
                        goods_img_num++;
                        if (_this.data("sign") == "single" || maxSum == goods_img_num) {
                            add_img.next('label').remove();
                            _this.siblings(".add_img").hide();
                        } //单图上传或者达到了上传的数量
                        $('.mud').find('div').text("上传成功");
                        setTimeout(function() {
                            $(".mud").hide();
                        }, 1000);
                    });
                };
                reader.readAsDataURL(file[0]);
            }
        });


        //删除分类图片
        $('body').on('click', 'a.del.del_img', function() {
            goods_img_num--;
            $(this).parents('.img_item').remove();
            $(".add_img").show();
            $(".mud").show().find('div').text("删除成功");
            setTimeout(function() {
                $(".mud").hide();
            }, 1000);
        });
    })

})